<?php
namespace pays\Gateways\alipay;
use pays\Gateways\alipay\AlipaySdk;
class Alipay {
    protected $config;
    public function __construct($config) {
        $this->config = $config;
    }
    //扫码支付
    public function qrcode($order = array()) {
        if (!is_array($order)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($order, ["total"])) {
            die("请传入付款金额!");
        }
        if (!$this->check_params($order, ["ordersn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($order, ["subject"])) {
            die("请传入付款描述参数!");
        }
        if (!$this->check_params($order, ["notify_url"])) {
            die("请传入异步回调地址参数!");
        }
        if (!$this->check_params($order, ["return_url"])) {
            die("请传入同步回调地址参数!");
        }
        $config = $this->config;
        $alipaysdk = new AlipaySdk();
        $alipaysdk->setAppid($config['app_id']);
        $alipaysdk->setMethod('alipay.trade.precreate');
        $alipaysdk->setNotifyUrl($order['notify_url']);
        $alipaysdk->setReturnUrl($order['return_url']);
        $alipaysdk->setRsaPrivateKey($config['merchant_private_key']);
        $alipaysdk->setTotalFee($order['total']);
        $alipaysdk->setOutTradeNo($order['ordersn']);
        $alipaysdk->setOrderName($order['subject']);
        $result = $alipaysdk->doPay();
        return $result;
    }
    //手机支付
    public function h5($order = array()) {
        if (!is_array($order)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($order, ["total"])) {
            die("请传入付款金额!");
        }
        if (!$this->check_params($order, ["ordersn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($order, ["subject"])) {
            die("请传入付款描述参数!");
        }
        if (!$this->check_params($order, ["notify_url"])) {
            die("请传入异步回调地址参数!");
        }
        if (!$this->check_params($order, ["return_url"])) {
            die("请传入同步回调地址参数!");
        }
        $config = $this->config;
        $alipaysdk = new AlipaySdk();
        $alipaysdk->setAppid($config['app_id']);
        $alipaysdk->setMethod('alipay.trade.wap.pay');
        $alipaysdk->setNotifyUrl($order['notify_url']);
        $alipaysdk->setReturnUrl($order['return_url']);
        $alipaysdk->setRsaPrivateKey($config['merchant_private_key']);
        $alipaysdk->setTotalFee($order['total']);
        $alipaysdk->setOutTradeNo($order['ordersn']);
        $alipaysdk->setOrderName($order['subject']);
        $alipaysdk->setGetPost('get');
        $result = $alipaysdk->doPay();
        return $result;
    }
    //订单查询
    public function query($input = array()) {
        if (!is_array($input)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($input, ["ordersn"])) {
            die("请传入支付单号!");
        }
        $config = $this->config;
        $alipaysdk = new AlipaySdk();
        $alipaysdk->setAppid($config['app_id']);
        $alipaysdk->setRsaPrivateKey($config['merchant_private_key']);
        $alipaysdk->setOutTradeNo($input['ordersn']);
        $result = $alipaysdk->doQuery();
        
        return $result;
    }
	//转账给用户
    public function transfer($input = array()) {
        if (!is_array($input)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($input, ["ordersn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($input, ["account"])) {
            die("请传入收款人账户!");
        }
        if (!$this->check_params($input, ["total"])) {
            die("请传入转账金额!");
        }
        $config = $this->config;
        $alipaysdk = new AlipaySdk();
        $alipaysdk->setAppid($config['app_id']);
        $alipaysdk->setRsaPrivateKey($config['merchant_private_key']);
        
        $alipaysdk->setOutTradeNo($input['ordersn']);
        $alipaysdk->setAccount($input['account']);
        $alipaysdk->setTotalFee($input['total']);
        $result = $alipaysdk->transfer();
        return $result;
    }

    //订单退款
    public function refund($input = array()) {

        if (!is_array($input)) {
            die("请传入数组参数!");
        }
        if (!$this->check_params($input, ["order_sn"])) {
            die("请传入订单编号!");
        }
        if (!$this->check_params($input, ["total_fee"])) {
            die("请传入订单支付金额!");
        }
        if (!$this->check_params($input, ["refund_reason"])) {
            die("请传入退款原因!");
        }
        return json_decode($input,true);
    }

    protected function check_params($data, $c = array()) {
        if (empty($c)) {
            return $data;
        }
        foreach ($c as $k => $v) {
            if (array_key_exists($v, $data) == false) {
                return false;
            }
        }
        return $data;
    }
}
